﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService.ViewDatas;
using DBService;

public partial class LatestReviews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindLatestReviewRepeater();
        BindFirtsReviewRepeater();
        BindTrandingReviewRepeater();
    }

    public void LatestReviewRep_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        var item = (ReviewInfo)e.Item.DataItem;

        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("LatestReviewItem");
            var diff = (DateTime.Now - item.createDate);
            var timeString = diff.TotalMinutes < 60 ? diff.Minutes + " წუთის წინ..." : item.createDate.ToShortDateString();
            string reviewText = item.review.Length > 155 ? item.review.Substring(0, 155) : item.review;

            string avatar = "";
            if (item.UserFB.HasValue)
            {
                avatar = String.Format("http://graph.facebook.com/{0}/picture?width=50&height=50", item.UserFB.ToString());
            }
            else
            {
                avatar = String.IsNullOrEmpty(item.avatar) ? "images/no-avatar.jpeg" : "upload/" + item.avatar;
            }
            lit.Text = "<div class=\"review_block\">"
                           + "<div class=\"review_img\">"
                               + "<img src=\"" + avatar + "\" alt=\"rev_img\" height=\"50\" width=\"50\">"
                          + "</div>"
                           + "<div class=\"review_content\">"
                               + "<div class=\"rev_name\">"
                                   + "<span><a href=\"UserProfile.aspx?id=" + item.userId + "\">" + item.userFullName + "</a></span> &rarr; <span><a href=\"SingleCompany.aspx?id=" + item.CompanyID + "\">" + item.companyName + "</a></span></div>"
                               + "<div class=\"rev_text\">"
                              + reviewText
                               + "</div>"
                               + "<div class=\"rev_time\">"
                                + timeString + "</div>"
                           + "</div>"
                           + "<div class=\"clear\">"
                           + "</div>"
                       + "</div>";
        }
    }

    public void BindLatestReviewRepeater()
    {
        var service = new ModelAdapter();
        var coll = service.GetLatestReviewForSearch(5);
        LatestReviewsRep.DataSource = coll;
        LatestReviewsRep.DataBind();
    }

    public void firstRevivewRepeater_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        var item = (FirstTimeReviews_Result)e.Item.DataItem;

        var lit = (Literal)e.Item.FindControl("firstRevivewRepeaterItem");
        var diff1 = Convert.ToDateTime((DateTime.Now - item.CreateDate == null ? DateTime.Now.AddDays(-20) : item.CreateDate.Value));
        TimeSpan diff = new TimeSpan(diff1.Hour, diff1.Minute, diff1.Second);
        var timeString = diff.TotalMinutes < 60 ? diff.Minutes + " წუთის წინ..." : diff1.ToShortDateString();
        string reviewText = item.Review.Length > 155 ? item.Review.Substring(0, 155) : item.Review;

        string avatar = "";
        if (item.FaceID.HasValue)
        {
            avatar = String.Format("http://graph.facebook.com/{0}/picture?width=50&height=50", item.FaceID.ToString());
        }
        else
        {
            avatar = String.IsNullOrEmpty(item.avatar) ? "images/no-avatar.jpeg" : "upload/" + item.avatar;
        }

        lit.Text = "<div class=\"review_block\">"
                                                    + "<div class=\"f_review_img\">"
                                                        + "<img src=\"" + avatar + "\" alt=\"rev_img\" height=\"50\" width=\"50\" />"
                                                    + "</div>"
                                                    + "<div class=\"f_content\">"
                                                        + "<div class=\"rev_name\">"
                                                            + "<span><a href=\"UserProfile.aspx?id=" + item.UserID + "\">" + item.userFullName + "</a></span> &rarr; <span><a href=\"SingleCompany.aspx?id=" + item.CompanyID + "\">" + item.Name + "</a></span></div>"
                                                        + "<div class=\"f_text\">"
                                                            + reviewText
                                                        + "</div>"
                                                        + "<div class=\"rev_time\">"
                                                            + timeString + "</div>"
                                                    + "</div>"
                                                    + "<div class=\"clear\">"
                                                    + "</div>"
                                                + "</div>";
    }

    public void TrandingRevivewRepeater_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        var item = (TrandingReviews_Result)e.Item.DataItem;

        var lit = (Literal)e.Item.FindControl("TrandingRevivewRepeaterItem");
        var diff1 = Convert.ToDateTime((DateTime.Now - item.CreateDate == null ? DateTime.Now.AddDays(-20) : item.CreateDate.Value));
        TimeSpan diff = new TimeSpan(diff1.Hour, diff1.Minute, diff1.Second);
        var timeString = diff.TotalMinutes < 60 ? diff.Minutes + " წუთის წინ..." : diff1.ToShortDateString();
        string reviewText = item.Review.Length > 155 ? item.Review.Substring(0, 155) : item.Review;

        string avatar = "";
        if (item.FaceID.HasValue)
        {
            avatar = String.Format("http://graph.facebook.com/{0}/picture?width=50&height=50", item.FaceID.ToString());
        }
        else
        {
            avatar = String.IsNullOrEmpty(item.avatar) ? "images/no-avatar.jpeg" : "upload/" + item.avatar;
        }

        lit.Text = "<div class=\"review_block\">"
                                                + "<div class=\"f_review_img\">"
                                                    + "<img src=\"" + avatar + "\" alt=\"rev_img\" height=\"50\" width=\"50\">"
                                                + "</div>"
                                                + "<div class=\"trand_content\">"
                                                    + "<div class=\"rev_name\">"
                                                       + "<span><a href=\"UserProfile.aspx?id=" + item.UserID + "\">" + item.userFullName + "</a></span> &rarr; <span><a href=\"SingleCompany.aspx?id=" + item.CompanyID + "\">" + item.Name + "</a></span></div>"
                                                    + "<div class=\"f_text\">"
                                                        + reviewText
                                                    + "</div>"
                                                    + "<div class=\"rev_time\">"
                                                        + timeString + "</div>"
                                                + "</div>"
                                                + "<div class=\"clear\">"
                                                + "</div>"
                                            + "</div>";
    }

    private void BindFirtsReviewRepeater()
    {
        var service = new ModelAdapter();
        var coll = service.GetFirstTimeReviews();
        firstRevivewRepeater.DataSource = coll;
        firstRevivewRepeater.DataBind();
    }

    private void BindTrandingReviewRepeater()
    {
        var service = new ModelAdapter();
        var coll = service.GetTrandingTimeReviews();
        TrandingRevivewRepeater.DataSource = coll;
        TrandingRevivewRepeater.DataBind();
    }

}